package com.zzld.modules.maintenance.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.abel533.entity.Example;
import com.github.pagehelper.PageInfo;
import com.zzld.core.utils.Base64Coder;
import com.zzld.core.utils.CookieUtils;
import com.zzld.modules.autoinn.entity.Area;
import com.zzld.modules.autoinn.entity.Collect;
import com.zzld.modules.autoinn.entity.Customer;
import com.zzld.modules.autoinn.entity.Store;
import com.zzld.modules.autoinn.service.IAreaService;
import com.zzld.modules.autoinn.service.IStoreService;
import com.zzld.modules.common.dto.AreaSelect;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.common.dto.NavCat;
import com.zzld.modules.common.dto.NavTab;
import com.zzld.modules.common.dto.SortSelect;

@Controller
@RequestMapping("/weixin/f/matenance/by/store")
public class WXMtanStoreController {
	
	private String storelist = "wx/matenance/storelist";
	
	@Autowired
	private IAreaService areaService;
	
	@Autowired
	private IStoreService storeService;
	
	@RequestMapping(method=RequestMethod.GET)
	public String store(HttpServletRequest request,Model model){
		
	
		String city = CookieUtils.getCookie(request, "str");
		
		if(city!=null){
			 if(city.indexOf("区")==-1){
				 model.addAttribute("city", areaService.selectbyid(city).getAreaname());
			 }else{
				 model.addAttribute("city", city);
			 }
		}else{
			return  "redirect:/weixin/f/matenance/by/brand/citylist";
		}
		
		// 获取父级区域参数和子集区域参数
		String area_f = request.getParameter("area_f");
		String area_sub = request.getParameter("area_sub");
		// 获取排序参数
		String sort = request.getParameter("sort");
		NavCat navCat = getNavCat(area_f, area_sub, sort);
		model.addAttribute("navCat", navCat);
		
	
		return storelist;
	}
	
	@RequestMapping("/pagelist")
	@ResponseBody
	public CommonJSON wxxcstore(Model model, HttpServletRequest request) {
		CommonJSON result = new CommonJSON();

		String lng = "";
		String lat = "";
		String lnglat = CookieUtils.getCookie(request, "latlng");

		Map<String, Object> map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(lnglat)){
			String[] po = lnglat.split(",");
		    lng = po[0].toString();
		    lat = po[1].toString();
			if(lng ==null|| lng.equals("null")){
				lng="0";
			}
			if(lat == null || lat.equals("null")){
				lat="0";
			}
			double longitude = Double.parseDouble(lng);
			double latitude = Double.parseDouble(lat);
			map.put("lat", latitude==0.0?null:latitude);
			map.put("lng", longitude==0.0?null:longitude);
		}
		else{
			map.put("lat", null);
			map.put("lng", null);
		}
		
	   
		
		
		// 区域ID
		String area_f = request.getParameter("area_f");
		String area_sub = request.getParameter("area_sub");
		String sou = request.getParameter("sou");

		if (area_f != null && area_f.equals("null")) {
			area_f = null;
		}

		if (area_sub != null && area_sub.equals("null")) {
			area_sub = null;
		}

		map.put("areaid", area_f);
		map.put("area_sub", area_sub);
	

	

		
		Integer pageNum = Integer
				.parseInt(request.getParameter("pageNum") == null ? "1"
						: request.getParameter("pageNum"));



		// 获取排序
		String sort = request.getParameter("sort") == null ? "jlzj" : request
				.getParameter("sort");
		if (sort.equals("null")) {
			sort = "jlzj";
		}
	
		// 排序为距离最近
		if (sort.equals("jlzj") || sort.equals("default")) {
			try {
				PageInfo<Store> storeList = storeService.selectbybyjuli(
						map, pageNum, 10);
				result.setSuccess(true);
				// 总条数
				result.setTotal(storeList.getTotal());
				result.setRows(storeList.getList());
			} catch (Exception e) {
				e.printStackTrace();
				result.setSuccess(false);
			}
		} else if (sort.equals("sold")) { // 评分
			try {
				PageInfo<Store> storeList = storeService.selectbybyfen(map,
						pageNum, 10);
				result.setSuccess(true);
				// 总条数
				result.setTotal(storeList.getTotal());
				result.setRows(storeList.getList());
			} catch (Exception e) {
				e.printStackTrace();
				result.setSuccess(false);
			}
		} 
		return result;

	}

	
	
	/**
	 * 获取筛选排序拼装
	 * 
	 * @param area_f
	 * @param area_sub
	 * @param sort
	 * @return
	 */
	private NavCat getNavCat(String area_f, String area_sub, String sort) {
		NavCat navCat = new NavCat(); // 返回拼装结果对象
		List<NavTab> navtabList = new ArrayList<NavTab>();
		List<AreaSelect> areaList = new ArrayList<AreaSelect>();
		List<SortSelect> sortList = new ArrayList<SortSelect>();

		String nav_tab_arae = "全部区域";
		String nav_tab_sort = "距离最近";
		

		if (StringUtils.isBlank(sort)) {
			sort = "default";
		}

		List<Area> area_p_list = null;
		// 查询区域父级并加载
		try {
			Example fexample = new Example(Area.class);
			fexample.createCriteria().andEqualTo("level", 2);
			area_p_list = areaService.selectByExample(fexample);

			// 拼接默认区域参数
			AreaSelect fas = new AreaSelect();
			fas.setText("全部区域");
			fas.setDatamain("area_emptyMenu");
			fas.setHref("/weixin/f/matenance/by/store?sort=" + sort);
			if (StringUtils.isBlank(area_f)) {
				fas.setSelect("select");
			}
			areaList.add(fas);

			if (area_p_list != null) {
				for (Area area : area_p_list) {
					List<Area> area_sub_list = new ArrayList<Area>();
					List<AreaSelect> subaslist = new ArrayList<AreaSelect>();

					// 拼接父级区域数据
					AreaSelect areaselect = new AreaSelect();
					areaselect.setDatamain("area_" + area.getId());
					areaselect.setText(area.getAreaname());
					areaselect.setHref("/weixin/f/matenance/by/store?area_f="
							+ area.getId() + "&sort=" + sort);
					if (StringUtils.isNotBlank(area_f)
							&& area_f.equals(area.getId())) {
						nav_tab_arae = area.getAreaname();
						areaselect.setSelect("select");
					}

					// 拼接子级区域数据
					Example subexample = new Example(Area.class);
					subexample.createCriteria().andEqualTo("pid", area.getId());
					area_sub_list = areaService.selectByExample(subexample);
					if (area_sub_list != null) {
						for (Area subarea : area_sub_list) {
							AreaSelect subareaselect = new AreaSelect();
							subareaselect
									.setHref("/weixin/f/matenance/by/store?area_f="
											+ area.getId()
											+ "&area_sub="
											+ subarea.getId() + "&sort=" + sort);
							subareaselect.setText(subarea.getAreaname());
							if (StringUtils.isNotBlank(area_sub)
									&& area_sub.equals(subarea.getId())) {
								nav_tab_arae = subarea.getAreaname();
								subareaselect.setSelect("select");
							}
							subaslist.add(subareaselect);
						}
						areaselect.setSubList(subaslist);
					} else {
						areaselect.setHref("/weixin/f/matenance/by/store?area_f="
								+ area.getId() + "&sort=" + sort);
						areaselect.setDatamain("area_emptyMenu");
					}

					areaList.add(areaselect);
				}
			}
			String sort_url = "/weixin/f/matenance/by/store?";
			if (StringUtils.isNotBlank(area_f) && StringUtils.isBlank(area_sub)) {
				sort_url += "area_f=" + area_f + "&";
			} else if (StringUtils.isBlank(area_f)
					&& StringUtils.isNotBlank(area_sub)) {
				sort_url += "area_sub=" + area_sub + "&";
			} else if (StringUtils.isNotBlank(area_f)
					&& StringUtils.isNotBlank(area_sub)) {
				sort_url += "area_f=" + area_f + "&area_sub=" + area_sub + "&";
			}
			// sort

			SortSelect mrSortSelect = new SortSelect();
			mrSortSelect.setText("距离最近");
			mrSortSelect.setHref(sort_url + "sort=jlzj");
			if (sort.equals("jlzj")) {
				nav_tab_sort = "距离最近";
				mrSortSelect.setSelect("select");
			}
			sortList.add(mrSortSelect);

			SortSelect sortSelect1 = new SortSelect();
			sortSelect1.setText("评分最高");
			sortSelect1.setHref(sort_url + "sort=sold");
			if (sort.equals("sold")) {
				nav_tab_sort = "评分最高";
				sortSelect1.setSelect("select");
			}
			sortList.add(sortSelect1);

		
			// 菜单显示项目
			NavTab nav_area_tab = new NavTab();
			nav_area_tab.setDataType("area");
			nav_area_tab.setText(nav_tab_arae);
			navtabList.add(nav_area_tab);

			NavTab nav_sort_tab = new NavTab();
			nav_sort_tab.setDataType("sort");
			nav_sort_tab.setText(nav_tab_sort);
			navtabList.add(nav_sort_tab);

		

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		navCat.setAreaList(areaList);
		navCat.setNavtabList(navtabList);
		navCat.setSortList(sortList);
		return navCat;
	}

}
